package com.flashcash.solu.view;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.flashcash.kotlin.utils.FirebaseAnalyticsConstants;
import com.flashcash.kotlin.utils.FirebaseAnalyticsUtils;
import com.flashcash.solu.R;
import com.flashcash.solu.app.base.BaseActivity;
import com.flashcash.solu.bean.ProgressBean;
import com.flashcash.solu.common.EventCollection;
import com.flashcash.solu.common.FieldParams;
import com.flashcash.solu.utils.ACache;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;


public class IntroduceActivity extends BaseActivity<IntroduceActPresenter> implements IntroduceActView {

    private Button btnGetLoan;
    private TextView tvLogin;
    private ACache aCache;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_introduce;
    }

    @Override
    protected void init() {
        RxBus.get().register(this);

        btnGetLoan = findViewById(R.id.btn_get_cash_now);
        tvLogin = findViewById(R.id.tv_login);

        if (isLogin()) {
            tvLogin.setVisibility(View.GONE);
        }

        btnGetLoan.setOnClickListener(v -> {
            FirebaseAnalyticsUtils.INSTANCE.logEvent(FirebaseAnalyticsConstants.Event.BTN_NEW_HOME_GET_CASH_NOW, null);
            if (!isLogin()) {
                toLogin();
            } else {
                mPresenter.updateCertifyState(true);
            }
        });

        tvLogin.setOnClickListener(v -> {//去登录
            FirebaseAnalyticsUtils.INSTANCE.logEvent(FirebaseAnalyticsConstants.Event.BTN_NEW_HOME_LOGIN, null);
            if (!isLogin()) {
                toLogin();
            } else {
                mPresenter.updateCertifyState(true);
            }
        });

        aCache = ACache.get(this);
        aCache.put(FieldParams.ACacheKeys.ACACHE_ADD_USER, "1");
    }

    /**
     * event posted from: called loginSuccess api successful/failed
     */
    @Subscribe
    public void onLoginSuccess(EventCollection.LoginSuccess event) {
        //登录成功,请求四步认证完成状态
        mPresenter.updateCertifyState(false);
        tvLogin.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        FirebaseAnalyticsUtils.INSTANCE.logEvent(FirebaseAnalyticsConstants.Event.EXPOSURE_NEW_HOME, null);
    }

    @Override
    protected IntroduceActPresenter initPresenterImpl() {
        return new IntroduceActPreImp();
    }

    /**
     * 完成四步认证
     *
     * @param event
     */
    @Subscribe
    public void onFinishCertify(EventCollection.FirstFinishCertifyEvent event) {
        finish();
    }

    @Override
    public void updateCertifyStateSuccess(ProgressBean progressBean) {
        if (progressBean.isCompleted()) {//完成四步认证，跳转到MainActivity
            MainActivity.startMainActivityFromIntroduce(this);
            finish();
        } else {//未完成四步认证，跳转到认证页面
            handlerUnFinishCertify(progressBean, false);
        }
//        handlerUnFinishCertify(progressBean, false);
    }
}
